package com.sun.config;

import com.sun.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080");
    }


    @Autowired
    private LoginInterceptor loginInterceptor;
    /**
     * path:就是uri地址可以使用通配符**
     *                     **：表示任意字符、文件或者多级目录和目录中的文件
     *                     /代表根，
     *                     /**表示所有资源都会被拦截
     *                     /user/**表示user目录下所有的资源
     */
    String[] addPathPatterns ={
            "/**"
    };
    //要排除的路径，说明不需要用户登录也可以访问
    String[] excludePathPatterns ={
            "/login","/register"
    };
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       /* registry.addInterceptor(loginInterceptor)
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns);*/
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(addPathPatterns)
        .excludePathPatterns(excludePathPatterns);
    }
}
